import '../src/generated/response/common_response.pb.dart' as $pb_common;
import 'package:fluttertoast/fluttertoast.dart';

abstract class ApiHandle {
  success(dynamic data);
  error(int statusCode, dynamic data);
  toast(String msg);
  confirm(String title, String ok);
}

class DefaultApiHandle extends ApiHandle {
  @override
  success(data) {

  }

  @override
  error(int statusCode, data) {

  }

  @override
  toast(String msg) {

  }

  @override
  confirm(String title, String ok) {

  }
}

class ProtoApiHandle extends ApiHandle {
  @override
  confirm(String title, String ok) {
    // TODO: implement confirm
    throw UnimplementedError();
  }

  @override
  error(int statusCode, data) {
    if (data is $pb_common.Response) {
      toast(data.msg == "" ? "请求成功" : data.msg);
    } else {
      toast("请求失败");
    }
  }

  @override
  success(data) {
    if (data is $pb_common.Response) {
      toast(data.msg == "" ? "请求成功" : data.msg);
    } else {
      error(0, data);
    }
  }

  @override
  toast(String msg) {
    Fluttertoast.showToast(msg:msg, timeInSecForIosWeb: 1);
  }

}